package com.example.communist_app_api.controller.uniapp;


import com.example.communist_app_api.db.pojoDto.Comment;
import com.example.communist_app_api.db.pojoDto.UserSoncomment;
import com.example.communist_app_api.service.*;
import com.example.communist_app_api.util.JwtUtils;
import com.example.communist_app_api.util.R;
import com.example.communist_app_api.vo.uniapp.req.CommentReq;
import com.example.communist_app_api.vo.web.resp.SensitiveWordApiResp;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/uniApp/comment/")
@Api("评论controller控制器")
public class UniAppCommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private WordService wordService;

    @Autowired
    private UserSoncommentService userSoncommentService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserCommentLikeService userCommentLikeService;


    @PostMapping("/insertComment")
    @ApiOperation("添加评论")
    public R insertComment(@RequestBody CommentReq req,HttpServletRequest httpServletRequest)
    {
        Comment comment = new Comment();
        Integer userId = getUserIdByWeb(httpServletRequest);
        comment.setUserId(userId);
        comment.setArticleId(req.getArticleId());
        comment.setAuditorId(0);
        String context = req.getContext();
        SensitiveWordApiResp resp = wordService.detectSensitiveWord(context, String.valueOf(userId));
        comment.setContext(context);
        Boolean isSensitiveWord = resp.getIsSensitiveWord();
        if (!isSensitiveWord){
            //false时没有敏感词出现，就新增，审核人就以系统审核即可
            commentService.insertComment(comment);
        }
        else {
            commentService.insertNoComment(userId,req.getArticleId(),req.getContext());
        }
        return R.ok("新增成功");
    }


    @PostMapping("/insertUserSonComment")
    @ApiOperation("添加子评论")
    public R insertUserSonComment(@RequestBody CommentReq req,HttpServletRequest httpServletRequest)
    {
        String userId = String.valueOf(getUserIdByWeb(httpServletRequest));
        String context = req.getContext();
        System.out.println(context);
        SensitiveWordApiResp resp = wordService.detectSensitiveWord(context,userId);
        Boolean isSensitiveWord = resp.getIsSensitiveWord();
        Integer auditorId = 0;
        if (!isSensitiveWord){
            //false时没有敏感词出现，就新增，审核人就以系统审核即可
            userSoncommentService.isInsertUserComment(req.getUserId(),req.getId(),context,auditorId);
        }
        else {
            userSoncommentService.insertUserComment(req.getUserId(),req.getId(),context);
        }
        return R.ok("新增成功");
    }


    @PostMapping("/commentLike")
    @ApiOperation("评论点赞")
    public R commentLike(@RequestBody CommentReq req,HttpServletRequest httpServletRequest)
    {
        Integer id = req.getId();
        int num = userCommentLikeService.insertUserCommentLike(getUserIdByWeb(httpServletRequest),id);
        System.out.println(id);
        boolean flag = commentService.UpdateLikeNum(id);
        if (num != 0 && flag){
                return R.ok();
        }
        else
        return R.error();
    }



    @PostMapping("/deleteCommentLike")
    @ApiOperation("取消评论点赞")
    public R deleteCommentLike(@RequestBody CommentReq req,HttpServletRequest httpServletRequest)
    {
        Integer id = req.getId();
        int num = userCommentLikeService.deleteUserCommentLike(getUserIdByWeb(httpServletRequest),id);
        boolean flag = commentService.deleteLikeNum(id);
        if (num != 0 && flag){
                return R.ok();
        }
        else
            return R.error();
    }

    private Integer getUserIdByWeb(HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        String id = claims.getSubject();
        int userId = Integer.parseInt(id);
        return userId;
    }




}
